//
//  PCDApiRedirectTo.m
//  PCDBank
//
//  Created by DK on 2021/2/25.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDApiRedirectTo.h"
#import "PCDBankWebViewController.h"
#import "PCDDataBaseModel.h"

@implementation PCDApiRedirectTo

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{

    WKWebView *webView = context.webView;
    PCDBankWebViewController *viewPage = (PCDBankWebViewController *)context.viewController;
    
    NSDictionary *tempDic = (NSDictionary *)data;
    NSString *callBack = [tempDic objectForKey:@"success"];
    NSString *strUrl = [tempDic objectForKey:@"url"];
    
    //packageCode bid 离线包编号
    NSString *appId = [tempDic objectForKey:@"appId"];
    //应用编号，appId
    NSString *appName = [tempDic objectForKey:@"appName"];
        NSString *strFrameworkType = [tempDic objectForKey:@"frameworkType"];
    
    NSString *extraData =[tempDic objectForKey:@"data"];
    [PCDDataBaseModel shareModel].extraData = extraData;
    
    if ([strUrl hasPrefix:@"/"]) {
        strUrl = [strUrl substringFromIndex:1];
    }
    
    if ([PCDUtil nilOrEmptyString:strFrameworkType]) {
        strFrameworkType = @"6";
    }
    if ([PCDUtil nilOrEmptyString:appName]) {
        appName =[PCDUtil channelId];
    }
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *params = @{@"bid":[PCDUtil safeString:appId],@"url":[PCDUtil safeString:strUrl],@"frameworkType":strFrameworkType,@"appId":[PCDUtil safeString:appName]};
    
    //添加页面随机数
    NSString *strSTime = [NSString stringWithFormat:@"?t=%@",[PCDUtil return16LetterAndNumber]];

    
    if (!PCDPageViewMangeGet().bOnLine)
    {
        [[PCDLoadPackageManage sharedService] loadSixPackageWithBid:params CompletionBlock:^(id  _Nonnull object, BOOL success, NSString * _Nonnull error) {
            //
            if (success) {

                dispatch_async(dispatch_get_main_queue(), ^{

                    NSDictionary *tempDic = (NSDictionary *)object;
                    NSString *pgAppId = tempDic[@"appId"];
                    NSString *pgBaseUrl = tempDic[@"baseUrl"];
                    
                    
                    if (PCDPageViewMangeGet().baseUrlList.count >0) {
                        [PCDPageViewMangeGet().baseUrlList removeLastObject];
                    }
                    
                    if (![viewPage.appId isEqual:pgAppId]) {
                        viewPage.appId = appId;
                        PCDPageViewMangeGet().baseUrl = pgBaseUrl;
                        PCDPageViewMangeGet().appId   = pgAppId;
                    }
                    
                    [PCDPageViewMangeGet().baseUrlList addObject:pgBaseUrl];
                    [PCDWServiceGet() loadWKWebViewWithParams:tempDic WebView:webView];
                   
                });
            }else{
                [viewPage.view makeToast:error];
            }
        }];
    }else{
        
        NSString *baseUrl = PCDPageViewMangeGet().baseUrl;
          
        if ([PCDUtil nilOrEmptyString:appId]) {
            NSString *strPageUrl = [NSString stringWithFormat:@"%@%@#%@",baseUrl,strSTime,strUrl];
            NSDictionary *params = @{@"appId": PCDPageViewMangeGet().appId,@"baseurl":baseUrl,@"url":strPageUrl};
            [PCDWServiceGet() loadWKWebViewWithParams:params WebView:webView];
            [webView reload];
            
        }else{
            
            if (![viewPage.appId isEqual:appId]) {

                NSString *strPageUrl = [NSString stringWithFormat:@"%@%@#%@",baseUrl,strSTime,strUrl];
                NSDictionary *params = @{@"appId":appId,@"baseurl":baseUrl,@"url":strPageUrl};
                
                viewPage.appId = appId;
                
                if (PCDPageViewMangeGet().baseUrlList.count >0) {
                    [PCDPageViewMangeGet().baseUrlList removeLastObject];
                }
                PCDPageViewMangeGet().baseUrl = baseUrl;
                PCDPageViewMangeGet().appId   = appId;
                [PCDWServiceGet() loadWKWebViewWithParams:params WebView:webView];
                [PCDPageViewMangeGet().baseUrlList addObject:baseUrl];
                
            }else{
                NSString *strPageUrl = PCDPageViewMangeGet().baseUrl;
                if (![PCDUtil nilOrEmptyString: strUrl]) {
                    strPageUrl = [NSString stringWithFormat:@"%@%@#%@",strPageUrl,strSTime,strUrl];
                }
                NSDictionary *params = @{@"appId": PCDPageViewMangeGet().appId,@"baseurl":PCDPageViewMangeGet().baseUrl,@"url":strPageUrl};
                [PCDWServiceGet() loadWKWebViewWithParams:params WebView:webView];
            }
        }
        
    }

    //执行页面回调
    if (![PCDUtil nilOrEmptyString:callBack]) {
        evaluateJavaScriptOnMainThread(self,callBack,@"");
    }
}

@end
